<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="公司名称" prop="gongSiName">
        <el-input
          v-model="queryParams.gongSiName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="xiangMuName">
        <el-input
          v-model="queryParams.xiangMuName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="lianXiRen">
        <el-input
          v-model="queryParams.lianXiRen"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="fuZeRen">
        <el-input
          v-model="queryParams.fuZeRen"
          placeholder="请输入项目负责人"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item label="安装人员" prop="anZhuangRen">
        <el-input
          v-model="queryParams.anZhuangRen"
          placeholder="请输入安装人员"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="beiZhu">
        <el-input
          v-model="queryParams.beiZhu"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['br5103:dingdan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['br5103:dingdan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['br5103:dingdan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['br5103:dingdan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dingdanList" @selection-change="handleSelectionChange" @expand-change="expandChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template #default="scope">
          <el-table :data="sysSheBeiList" >
           <el-table-column label="ID" prop="id" align="center"/>
            <el-table-column label="SN" prop="sn" align="center"/>
            <el-table-column label="出货日期" prop="chuHuoDate" align="center"/>
            <el-table-column label="质保到期" prop="zhiBaoDaoQi" align="center"/>
            <el-table-column label="备注" prop="beiZhu" align="center"/>
          </el-table>
        </template>
      </el-table-column>
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="公司名称" align="center" prop="gongSiName" />
      <el-table-column label="项目名称" align="center" prop="xiangMuName" />
      <el-table-column label="联系人" align="center" prop="lianXiRen" />
      <el-table-column label="联系人手机" align="center" prop="lianXiRenPhone" />
      <el-table-column label="收货人" align="center" prop="shouHuoRen" />
      <el-table-column label="收货人手机" align="center" prop="shouHuoRenPhone" />
      <el-table-column label="项目地址" align="center" prop="address" />
      <el-table-column label="项目负责人" align="center" prop="fuZeRen" />
      <el-table-column label="安装人员" align="center" prop="anZhuangRen" />
      <el-table-column label="订单日期" align="center" prop="date" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成日期" align="center" prop="wanChengDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.wanChengDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="beiZhu" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['br5103:dingdan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['br5103:dingdan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改dingdan对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司名称" prop="gongSiName">
          <el-input v-model="form.gongSiName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="项目名称" prop="xiangMuName">
          <el-input v-model="form.xiangMuName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="lianXiRen">
          <el-input v-model="form.lianXiRen" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人手机" prop="lianXiRenPhone">
          <el-input v-model="form.lianXiRenPhone" placeholder="请输入联系人手机" />
        </el-form-item>
        <el-form-item label="收货人" prop="shouHuoRen">
          <el-input v-model="form.shouHuoRen" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货人手机" prop="shouHuoRenPhone">
          <el-input v-model="form.shouHuoRenPhone" placeholder="请输入收货人手机" />
        </el-form-item>
        <el-form-item label="项目地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入项目地址" />
        </el-form-item>
        <el-form-item label="项目负责人" prop="fuZeRen">
          <el-input v-model="form.fuZeRen" placeholder="请输入项目负责人" />
        </el-form-item>
        <el-form-item label="安装人员" prop="anZhuangRen">
          <el-input v-model="form.anZhuangRen" placeholder="请输入安装人员" />
        </el-form-item>
        <el-form-item label="完成日期" prop="wanChengDate">
          <el-date-picker clearable
            v-model="form.wanChengDate"
            type="date"
            placeholder="请选择完成日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="beiZhu">
          <el-input type="textarea" v-model="form.beiZhu" placeholder="请输入备注"></el-input>
        </el-form-item>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" size="mini" @click="handleAddSysSheBei">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" size="mini" @click="handleDeleteSysSheBei">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="sysSheBeiList" :row-class-name="rowSysSheBeiIndex" @selection-change="handleSysSheBeiSelectionChange" ref="sysSheBei">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="SN" prop="sn" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.sn" placeholder="请输入SN" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="beiZhu" width="150">
            <template #default="scope">
              <el-input type="textarea" v-model="scope.row.beiZhu" placeholder="请输入备注" size="small"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDingdan, getDingdan, delDingdan, addDingdan, updateDingdan } from "@/api/system/dingdan";

export default {
  name: "Dingdan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysSheBei: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // dingdan表格数据
      dingdanList: [],
      // ${subTable.functionName}表格数据
      sysSheBeiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gongSiName: null,
        xiangMuName: null,
        lianXiRen: null,
        fuZeRen: null,
        anZhuangRen: null,
        beiZhu: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dingDanHao: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        gongSiName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        xiangMuName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        lianXiRen: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        lianXiRenPhone: [
          { required: true, message: "联系人手机不能为空", trigger: "blur" }
        ],
        shouHuoRenPhone: [
          { required: true, message: "收货人手机不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "项目地址不能为空", trigger: "blur" }
        ],
        fuZeRen: [
          { required: true, message: "项目负责人不能为空", trigger: "blur" }
        ],
        anZhuangRen: [
          { required: true, message: "安装人员不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "订单日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询dingdan列表 */
    getList() {
      this.loading = true;
      listDingdan(this.queryParams).then(response => {
        this.dingdanList = response.rows;
       // console.log(this.dingdanList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    expandChange(row){
      const id=row.id;
      getDingdan(id).then(response => {
        this.sysSheBeiList = response.data.sysSheBeiList;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dingDanHao: null,
        gongSiName: null,
        xiangMuName: null,
        lianXiRen: null,
        lianXiRenPhone: null,
        shouHuoRen: null,
        shouHuoRenPhone: null,
        address: null,
        fuZeRen: null,
        anZhuangRen: null,
        date: null,
        wanChengDate: null,
        beiZhu: null
      };
      this.sysSheBeiList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加dingdan";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDingdan(id).then(response => {
        this.form = response.data;
        this.sysSheBeiList = response.data.sysSheBeiList;
        this.open = true;
        this.title = "修改dingdan";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysSheBeiList = this.sysSheBeiList;
          if (this.form.id != null) {
            updateDingdan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDingdan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除dingdan编号为"' + ids + '"的数据项？').then(function() {
        return delDingdan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowSysSheBeiIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddSysSheBei() {
      let obj = {};
      obj.sn = "";
      obj.chuHuoDate = "";
      obj.zhiBaoDaoQi = "";
      obj.beiZhu = "";
      this.sysSheBeiList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteSysSheBei() {
      if (this.checkedSysSheBei.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const sysSheBeiList = this.sysSheBeiList;
        const checkedSysSheBei = this.checkedSysSheBei;
        this.sysSheBeiList = sysSheBeiList.filter(function(item) {
          return checkedSysSheBei.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysSheBeiSelectionChange(selection) {
      this.checkedSysSheBei = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('br5103/dingdan/export', {
        ...this.queryParams
      }, `dingdan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
